package cn.com.victorysoft.xqfx.dao.impl;


import cn.com.victorysoft.oadd.util.StringUtil;
import cn.com.victorysoft.xqfx.bean.XqfxBean;
import cn.com.victorysoft.xqfx.dao.XqfxDAO;
import com.ibatis.sqlmap.client.SqlMapClient;
import config.SysConfig;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by adagio on 16/3/22.
 */
public class XqfxDAOImpl extends SqlMapClientDaoSupport implements XqfxDAO {
    // 获取一级组织结构数
    public List getTreeList() {
        return this.getSqlMapClientTemplate().queryForList(
                "xqfx.getTreeList", new HashMap());
    }

    // 获取二级组织机构数
    public List getTreeSonList(String id) {
        return this.getSqlMapClientTemplate().queryForList(
                "xqfx.getTreeSonList", id);
    }
    //获取tjid为空的时候最大的tjid
    public List getMaxTjidList(String id){
        return this.getSqlMapClientTemplate().queryForList("xqfx.getMaxTjidList",id);
    }
    // 增加分类节点
    public boolean addXqfx(XqfxBean xqfxBean) {
        xqfxBean.setBzflId(StringUtil.getUUID());
        Object obj = this.getSqlMapClientTemplate().insert(
                "xqfx.addXqfx", xqfxBean);
        return true;
    }

    // 删除分类节点
    public boolean deletXqfx(String id) {
        int rows = this.getSqlMapClientTemplate().delete(
                "xqfx.deletXqfx", id);
        if (rows > 0) {
            return true;
        }
        return false;
    }

    // 根据id查询判断是新增还是修改
    public List getDatagrid(String id) {
        return this.getSqlMapClientTemplate().queryForList(
                "xqfx.getDatagrid", id);
    }

    // 修改分类节点
    public boolean updateXqfx(XqfxBean xqfxBean) {
        Object obj = this.getSqlMapClientTemplate().update(
                "xqfx.updateXqfx", xqfxBean);
        return true;
    }

    // 上移
    public boolean MoveUp(String id, String pxz, String sjid) {
        Map m = new HashMap();
        int a = Integer.parseInt(pxz);
        m.put("pxzUp", a - 1);
        m.put("pxz", pxz);
        m.put("sjid", sjid);
        Map map = new HashMap();
        map.put("id", id);
        map.put("pxz", a - 1);
        map.put("sjid", sjid);
        SqlMapClient sqlMapClient = this.getSqlMapClientTemplate()
                .getSqlMapClient();
        try {
            sqlMapClient.startTransaction();
            // 1.上移首先修改排序值-1的节点改成当前操作节点的排序值(上移把上级节点+1和操作节点的排序值相同)
            this.getSqlMapClientTemplate().update("xqfx.MoveUp", m);
            // 2.根据id修改操作节点的排序值-1(把相同的排序值得根据id区分，-1操作节点上移)
            this.getSqlMapClientTemplate().update("xqfx.MoveUp_", map);
            sqlMapClient.commitTransaction();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw new RuntimeException(e);
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                // Log.error("CorrelateAlarmBO-->do***方法出现异常！" +
                // e.getMessage());
                throw new RuntimeException(e);
            }
        }
        return true;
    }

    // 下移 1,
    public boolean MoveDown(String id, String pxz, String sjid) {
        Map m = new HashMap();
        int a = Integer.parseInt(pxz);
        m.put("pxzDown", a + 1);
        m.put("pxz", pxz);
        m.put("sjid", sjid);
        Map map = new HashMap();
        map.put("id", id);
        map.put("pxzDown_", a + 1);
        map.put("sjid", sjid);
        SqlMapClient sqlMapClient = this.getSqlMapClientTemplate()
                .getSqlMapClient();
        try {
            sqlMapClient.startTransaction();
            // 1.下移先把要操作几点的排序值改为和操作节点相同的排序值
            this.getSqlMapClientTemplate().update("xqfx.MoveDown", m);
            // 2.根据id区分要下移的节点排序值+1
            this.getSqlMapClientTemplate().update("xqfx.MoveDown_", map);
            sqlMapClient.commitTransaction();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw new RuntimeException(e);
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                // Log.error("CorrelateAlarmBO-->do***方法出现异常！" +
                // e.getMessage());
                throw new RuntimeException(e);
            }
        }
        return true;
    }

    // 左移右移、
    public boolean Move_L_R(String id, String sjid) {

        SqlMapClient sqlMapClient = this.getSqlMapClientTemplate()
                .getSqlMapClient();
        try {
            sqlMapClient.startTransaction();
            // 1.首先根据sjid查询出最大的排序值
            List list = getList(sjid);
            int s = 0;
            if (list.size() > 0) {
                Map map = (Map) list.get(0);
                String pxz = map.get("ORDER_NUMBER") + "";
                s = Integer.parseInt(pxz);
            }

            // 取到最大的排序值+1,移动后的排序值
            int pxz = s + 1;
            // 修改操作节点排序值
            Map m = new HashMap();
            m.put("id", id);
            m.put("pxz", pxz);
            // 1.根据要操作节点的id修改排序值
            this.getSqlMapClientTemplate().update("xqfx.MoveLeftPxz", m);
            Map m1 = new HashMap();
            m1.put("id", id);
            m1.put("sjid", sjid);
            // 2.修改操作节点的上级id
            this.getSqlMapClientTemplate().update("xqfx.MoveLeft", m1);
            sqlMapClient.commitTransaction();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            throw new RuntimeException(e);
        } finally {
            try {
                sqlMapClient.endTransaction();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                // Log.error("CorrelateAlarmBO-->do***方法出现异常！" +
                // e.getMessage());
                throw new RuntimeException(e);
            }
        }
        return true;
    }

    public List getList(String id) {
        return this.getSqlMapClientTemplate().queryForList(
                "xqfx.getList", id);
    }
    public List getList1(String id) {
        return this.getSqlMapClientTemplate().queryForList(
                "xqfx.getList1", id);
    }
}
